﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace FirmaTransportowa
{
    public class Occupation : IModel<Occupation>
    {
        [System.ComponentModel.DisplayName("ID zawodu")]
        public int id { get; set; }
        [System.ComponentModel.DisplayName("Nazwa zawodu")]
        public String name { get; set; }

        public Occupation(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public Occupation()
        {

        }

        public string DisplayText
        {
            get
            {
                return this.name;
            }
        }

        public int ReturnValue
        {
            get
            {
                return this.id;
            }
        }

        public int save()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            int createdId = -1;

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                bool newObject = false;

                if (Occupation.getById(this.id) == null)
                {
                    cmd.CommandText = "INSERT INTO Occupation(name) VALUES(@name) SELECT CONVERT(int, SCOPE_IDENTITY())";
                    newObject = true;
                }
                else
                {
                    cmd.CommandText = "UPDATE Occupation SET name=@name WHERE occupation_ID=@id";
                    cmd.Parameters.AddWithValue("@id", this.id);
                }

                cmd.Parameters.AddWithValue("@name", this.name);
                cmd.Connection = conn;

                if (newObject)
                {
                    createdId = (int)cmd.ExecuteScalar();
                }
                else 
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }

            return createdId;
        }

        public static IList<Occupation> list()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            IList<Occupation> list = new List<Occupation>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Occupation", conn);
                SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.Default);

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Occupation occupation = Occupation.getById(int.Parse(reader["occupation_ID"].ToString()));

                        list.Add(occupation);
                    }
                }
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }

            return list;
        }


        public static Occupation getById(int id)
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            try
            {
                if (id < 0)
                {
                    throw new ArgumentException();
                }

                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SELECT * FROM Occupation WHERE occupation_ID=@id;";
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Connection = conn;
                SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);

                if (reader.HasRows)
                {
                    reader.Read();
                    Occupation occupation = new Occupation();
                    occupation.id = int.Parse(reader["occupation_ID"].ToString());
                    occupation.name = reader["name"].ToString();

                    conn.Close();
                    return occupation;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                conn.Close();

            }

            return null;

        }

        public void delete()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            try
            {
                conn.Open();
                conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "DELETE FROM Occupation WHERE occupation_ID=@id";
                cmd.Parameters.AddWithValue("@id", this.id);

                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
